<?php

namespace app\index\diyclass;
use app\index\model\IntergralLog;
use app\index\model\UserModel;

class Interal
{
    private $user = null;
    private $type = '';
    private $num = 1;
    private $aboutId = '';
    private $content = '';
    public function __construct($userid, $type)
    {
        $sql = new UserModel();
        $this->user = $sql->where('id', $userid)->findOrEmpty();
        $this->type = $type;
    }

    public function setNum($num)
    {
        $this->num = $num;
    }

    public function setContent($content)
    {
        $this->content = $content;
    }

    public function setAboutId($aboutId)
    {
        $this->aboutId = $aboutId;
    }

    public function save() {
        $this->user->integral = $this->user->integral + $this->num;
        if($this->type == 'INVITE') {
            $sql = new UserModel();
            $count = $sql->where('superiorUserId', $this->user->id)->count();
            if($count == 14) {
                $this->user->user_type = 'VIP';
            }
        }
        $this->user->save();
        $logSql = new IntergralLog();
        $logSql->userId = $this->user->id;
        $logSql->integral = $this->num;
        $logSql->allIntegral = $this->user->integral;
        $logSql->type = $this->type;
        $logSql->about_id = $this->aboutId;
        $logSql->content = $this->content;
        $logSql->save();
    }
}